

<template>
    <div @click="startUpload">
      <input type="file" name="file" ref="file" class="file"  @change="uploadDataFile">
      <slot  > </slot>
    </div>
</template>

<script>
import { todouplodafile } from '@/api/api'
export default {
  data(){
    return {

    }
  },
  methods:{
    async uploadDataFile(){
        const file = this.$refs.file.files[0]
        console.log(file)
        const data = new FormData()  // 手动实例一个JS表单对象
        data.append('file',file)
        let res = await todouplodafile(data)
        if(res.code==200){
          this.$emit('getfilepath',res.path)  // 相对于服务器的图片路径
        }
    },
    startUpload(){
      // console.log('startUpload')
      this.$refs.file.click()   // 事件回调
    }
  }
}
</script>

<style lang="scss" >
.file{
  display: none;
}
</style>
